package com.vhly.apb;

import android.content.Context;
import android.database.AbstractCursor;
import android.database.Cursor;
import android.util.Log;
import android.widget.SimpleCursorAdapter;
import com.vhly.remoteapi.yahoo.contact.Contact;
import com.vhly.remoteapi.yahoo.contact.ContactObject;
import com.vhly.remoteapi.yahoo.contact.Field;

import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-12-12
 * Time: 20:59:38
 * Email: vhly@163.com
 */
public class ContactCursor extends AbstractCursor {

    private static String[] colNames = {"_id", "CNAME", "CNUMBER", "CEMAIL"};

    private Vector<ContactObject> cos;

    public ContactCursor(Vector<ContactObject> objs) {
        cos = new Vector<ContactObject>();
        cos.addAll(objs);
    }

    public void updateData(Vector<ContactObject> objs){
        if(objs != null){
            cos.removeAllElements();
            cos.addAll(objs);
        }
    }

    @Override
    public String[] getColumnNames() {
        return colNames;
    }

    @Override
    public int getCount() {
        return cos.size();
    }

    @Override
    public String getString(int i) {
        Log.d("ContactCursor","getString "+i);
        String ret = null;
        if (!mClosed) {
            int size = cos.size();
            if (mPos < size) {
                ContactObject co = cos.get(mPos);
                if (co instanceof Contact) {
                    Contact ct = (Contact) co;
                    Field fd;
                    switch (i) {
                        case 0:
                            ret = co.id;
                            break;
                        case 1:
                            fd = ct.findNameField();
                            if(fd != null){
                                ret = fd.value.toString();
                            }else{
                                fd = ct.findNicknameField();
                                if(fd != null){
                                    ret = fd.value.toString();
                                }else{
                                    ret = "Noname";
                                }
                            }
                            break;
                        case 2:
                            fd = ct.findNumberField();
                            if(fd != null){
                                ret = fd.value.toString();
                            }
                            break;
                        case 3:
                            fd = ct.findEmailField();
                            if(fd != null){
                                ret = fd.value.toString();
                            }
                            break;
                    }
                }
            }
        }
        return ret;
    }

    @Override
    public short getShort(int i) {
        Log.d("ContactCursor","getShort "+i);
        if (!mClosed) {
            int size = cos.size();
            if (mPos < size) {
                ContactObject co = cos.get(mPos);
                switch (i) {
                    case 0:
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                }
            }
        }
        return 0;
    }

    @Override
    public int getInt(int i) {

        Log.d("ContactCursor","getInt "+i);
        if (!mClosed) {
            int size = cos.size();
            if (mPos < size) {
                ContactObject co = cos.get(mPos);
                switch (i) {
                    case 0:
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                }
            }
        }
        return 0;
    }

    @Override
    public long getLong(int i) {

        Log.d("ContactCursor","getLong "+i);
        if (!mClosed) {
            int size = cos.size();
            if (mPos < size) {
                ContactObject co = cos.get(mPos);
                switch (i) {
                    case 0:
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                }
            }
        }
        return 0;
    }

    @Override
    public float getFloat(int i) {

        Log.d("ContactCursor","getFloat "+i);
        if (!mClosed) {
            int size = cos.size();
            if (mPos < size) {
                ContactObject co = cos.get(mPos);
                switch (i) {
                    case 0:
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                }
            }
        }
        return 0;
    }

    @Override
    public double getDouble(int i) {
        long ret = 0;
        Log.d("ContactCursor","getDouble "+i);
        if (!mClosed) {
            int size = cos.size();
            if (mPos < size) {
                ContactObject co = cos.get(mPos);
                switch (i) {
                    case 0:
                        ret = Long.parseLong(co.id);
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                }
            }
        }
        return ret;
    }

    @Override
    public boolean isNull(int i) {

        Log.d("ContactCursor","isNull "+i);
        boolean ret = true;
        if (!mClosed) {
            int size = cos.size();
            if (mPos < size) {
                ContactObject co = cos.get(mPos);
                if (co instanceof Contact) {
                    Contact ct = (Contact) co;
                    switch (i) {
                        case 0:
                            ret = co.id == null;
                            break;
                        case 1:
                            ret = ct.findNameField() == null;
                            break;
                        case 2:
                            ret = ct.findNumberField() == null;
                            break;
                        case 3:
                            ret = ct.findEmailField() == null;
                            break;
                    }
                }
            }
        }
        return ret;
    }


}
